extern crate fejpsl_contract;
use fejpsl_contract::friend::{Messager,FriendMessage};

pub async fn messager_list(page_no:&i32,user_uuid:&String) -> Option<Vec<Messager>> {
    let client =super::db::connect().await.unwrap();
    let mut vec_messager = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("select b.friend_uuid,c.nice_name,c.real_name,c.is_explicit,c.avatar_text from
        (select friend_uuid from ((SELECT receive_user_uuid friend_uuid FROM public.t_friend_message where
         send_user_uuid=$3) union all
        (select send_user_uuid friend_uuid from public.t_friend_message where
        receive_user_uuid=$3)) a group by friend_uuid) b left join t_user c
        on b.friend_uuid=c.user_uuid limit $1 offset $2",
                             &[&per_page_num,&offset,&user_uuid]).await.unwrap();
    for row in rows {
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(2);
        if let Some(_i) = tmp_str {m_real_name= row.get(2); }else{ m_real_name=String::from("") }
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(4);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(4); }else{ m_avatar_text=String::from("") }
        let messager = Messager {
            friend_uuid:   row.get(0),
            nice_name: row.get(1),
            real_name:  m_real_name,
            is_explicit:   row.get(3),
            avatar_text: m_avatar_text,
        };
        vec_messager.push(messager);
    }
    return Some(vec_messager);
}
pub async fn friend_message_list(page_no:&i32,send_user_uuid:&String,receive_user_uuid:&String) -> Option<Vec<FriendMessage>> {
    let client =super::db::connect().await.unwrap();
    let mut vec_friend_message = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("select b.user_uuid,b.nice_name,b.real_name,b.is_explicit,b.avatar_text,a.content,a.create_time
        from ((SELECT * FROM public.t_friend_message where send_user_uuid=$3
        and receive_user_uuid=$4) union all
        (SELECT * FROM public.t_friend_message where send_user_uuid=$4
        and receive_user_uuid=$3)) a left join t_user b on
        a.send_user_uuid=b.user_uuid order by a.create_time desc limit $1 offset $2",
                             &[&per_page_num,&offset,&send_user_uuid,&receive_user_uuid]).await.unwrap();
    for row in rows {
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(2);
        if let Some(_i) = tmp_str {m_real_name= row.get(2); }else{ m_real_name=String::from("") }
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(4);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(4); }else{ m_avatar_text=String::from("") }
        let mut m_content=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_content= row.get(5); }else{ m_content=String::from("") }
        let friend_message = FriendMessage {
            user_uuid:   row.get(0),
            nice_name: row.get(1),
            real_name:  m_real_name,
            is_explicit:   row.get(3),
            avatar_text: m_avatar_text,
            content:m_content,
            create_time:row.get(6),
        };
        vec_friend_message.push(friend_message);
    }
    return Some(vec_friend_message);
}


